package com.yun.baymax.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.yun.baymax.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/12.
 */

public class PictureSelectedAdapter extends RecyclerView.Adapter<PictureSelectedAdapter.MyHolder> {
    Context context;
    List<String> datas = new ArrayList<>();
    List<String> removes = new ArrayList<>();
    private LayoutInflater inflater;

    public PictureSelectedAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_pick_pic, parent, false);
        MyHolder myHolder = new MyHolder(view);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        if (!TextUtils.isEmpty(datas.get(position)))
            holder.ivPic.setImageURI(Uri.fromFile(new File(datas.get(position))));
        //Glide.with(context).load(headImageUrl).into(holder.ivPic);
        //TODO
        holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removes.add(datas.get(position));
                datas.remove(position);
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public List<String> getRemoves() {
        return removes;
    }

    public List<String> getDatas() {
        return datas;
    }

    public void setDatas(List<String> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        private ImageView ivPic;
        private ImageView ivDelete;

        public MyHolder(View itemView) {
            super(itemView);
            ivPic = (ImageView) itemView.findViewById(R.id.iv_pic);
            ivDelete = (ImageView) itemView.findViewById(R.id.iv_delete);
        }
    }
}